package com.yejiarong.common.tools;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author YeJR
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class HttpResult<T> implements Serializable {

    /**
     * code
     */
    private Integer code = 200;

    /**
     * message
     */
    private String msg = "SUCCESS";

    /**
     * data
     * 属性为对象时也需要实现序列化
     */
    private T data;

    /**
     * success
     * @return HttpResult
     */
    public static <T> HttpResult<T> success(){
        return new HttpResult<T>(200, "SUCCESS", null);
    }

    /**
     * success
     * @param data  数据
     * @return HttpResult
     */
    public static <T> HttpResult<T> success(T data){
        return new HttpResult<T>(200, "SUCCESS", data);
    }

    /**
     * error
     * @param msg 异常信息
     * @return HttpResult
     */
    public static <T> HttpResult<T> error(String msg){
        return new HttpResult<T>(500, msg, null);
    }


    /**
     * error
     * @param code 异常码
     * @param msg 异常信息
     * @return HttpResult
     */
    public static <T> HttpResult<T> error(Integer code, String msg){
        return new HttpResult<T>(code, msg, null);
    }


    @JsonIgnore
    public boolean isSuccess(){
        return code == 200;
    }
}
